require 'yaml'
require "selenium-webdriver"

module RuSSY
  class TestConfig
      @@single_instance = nil
      def self.getinstance(settings = nil)
        if @@single_instance.nil?
          @@single_instance = TestConfig.new(settings)
          @@single_instance.load_data
        end
        @@single_instance
      end

      @langdata
      @locdata
      @testdata
      attr_accessor :locfilepath, :testdatafilepath, :langfilepath, :lang, :browser, :rcserver, :browsertype
      
      def load_data()
        # init test data
        if @testdatafilepath.nil?
          @testdatafilepath = "testdata.yml"
        end
        @testdata = YAML.load_file(@testdatafilepath)

        # init locator data
          if @locdatafilepath.nil?
            @locdatafilepath = "locators.yml"
          end
          @locdata = YAML.load_file(@locdatafilepath)

        # init lang data
          if @langfilepath.nil?
            if @lang.nil?
              @lang = "en"
            end
            @langfilepath = "lang_" + @lang + ".yml"
          end
          @langdata = YAML.load_file(@langfilepath)
      end

      def getstatusdata(strindex)
        hashvalue = @testdata["statusdata"]
        strindex.split('.').each {|key| hashvalue = hashvalue[key] }
        hashvalue
      end

      def getactiondata(strindex)
        hashvalue = @testdata["actiondata"]
        strindex.split('.').each {|key| hashvalue = hashvalue[key] }
        hashvalue
      end

      def getenvdata(strindex)
        hashvalue = @testdata["envdata"]
        strindex.split('.').each {|key| hashvalue = hashvalue[key] }
        hashvalue
      end

      def gettext(strindex)
        hashvalue = @langdata["lang"]
        hashvalue = hashvalue[@lang]
        strindex.split('.').each {|key| hashvalue = hashvalue[key] }
        hashvalue
      end

      def getlocator(strindex)
        hashvalue = @locdata["locators"]
        strindex.split('.').each {|key| hashvalue = hashvalue[key] }
        hashvalue
      end

      def find_element(elementname)
        locstring = getlocator(elementname)
        #puts "elementname is: " + elementname
        #puts "locstring is: " + locstring
        locelement = locstring.split("==")
        #puts "search way is: " + locelement[0]
        #puts "search value is: " + locelement[1]
        @browser.find_element(locelement[0], locelement[1])
      end

      def start_browser()
        #@browser = Selenium::WebDriver.for(:chrome)
        case @browsertype
          when "firefox"
            @browser = Selenium::WebDriver.for(:remote, :url => @rcserver, :desired_capabilities=>:firefox)
          when "chrome"
            @browser = Selenium::WebDriver.for(:remote, :url => @rcserver, :desired_capabilities=>:chrome)
          when "ie"
            @browser = Selenium::WebDriver.for(:remote, :url => @rcserver, :desired_capabilities=>:internet_explorer)
        end
      end

      def stop_browser()
        @browser.quit
        @browser = nil
      end
      
      def initialize(settings)
        if settings.nil?
          @lang = ENV['LANG'] || "en"
          @langfilepath = ENV['LANGFILEPATH'] || "lang_en.yml"
          @locfilepath = ENV['LOCFILEPATH'] || "locator.yml"
          @testdatafilepath = ENV['TESTDATAFILEPATH'] || "testdata.yml"
          @rcserver = ENV['RCSERVER'] || "http://localhost:4444/wd/hub"
          @browsertype = ENV['BROWSERTYPE'] || "firefox"
        else
          @lang = settings["lang"] || ENV['LANG'] || "en"
          @langfilepath = settings["langfilepath"] || ENV['LANGFILEPATH'] || "lang_en.xml"
          @locfilepath = settings["locfilepath"] || ENV['LOCFILEPATH'] || "locator.xml"
          @testdatafilepath = settings["testdatafilepath"] || ENV['TESTDATAFILEPATH'] || "testdata.xml"
          @rcserver = settings["rcserver"] || ENV['RCSERVER'] || "http://localhost:4444/wd/hub"
          @browsertype = settings["browsertype"] || ENV['BROWSERTYPE'] || "firefox"
        end
        @browser = nil
      end

      private :initialize

  end
end
